<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <div id="app"></div>

    <template id="my-app">
      <h2>{{info.name}}</h2>
      <button @click="changeInfo">改变info</button>
      <button @click="changeInfoName">改变info.name</button>
      <button @click="changeInfoNbaName">改变info.nba.name</button>
    </template>

    <script src="../js/Vue.js"></script>
    <script>
      const app = {
        template: '#my-app',
        data() {
          return {
            info: {
              name: '浮幻',
              age: 17,
              nba: {
                name: 'kobe'
              }
            }
          }
        },
        watch: {
          // 监听一个对象的属性，要侦听的写到引号里面
          'info.name'(newInfo, oldInfo) {
            console.log('newInfo：', newInfo, 'oldInfo：', oldInfo)
          }
        },
        methods: {
          changeInfo() {
            this.info = { name: 'fh' }
          },
          changeInfoName() {
            this.info.name = 'fh'
          },
          changeInfoNbaName() {
            this.info.nba.name = 'nba'
          }
        },
        created() {
          const unwatch = this.$watch(
            'info',
            (newInfo, oldInfo) => {
              console.log(newInfo, oldInfo)
            },
            {
              deep: true,
              immediate: true
            }
          )
          // unwatch()
        }
      }

      Vue.createApp(app).mount('#app')
    </script>
  </body>
</html>
